package com.ds.base.utils;

import com.ds.base.msg.ACBTGetMsg;
import com.ds.base.msg.ACBTMsg;
import com.ds.base.msg.ACBTPostMsg;
import com.ds.base.msg.OrderDeclaraMsg;
import com.value.exception.BusinessException;
import com.value.utils.DateUtil;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 *
 * @author zhangyou
 * @date 2018/12/2
 */
public class CBTUtil {

    public static String CBT_DOMAIN = "";

    public static String CONAME = "上海瑞奔景恒生物科技有限公司";
    public static String COCODE = "NWqw3Zm";
    public static String CBT_PRIVATE_KEY="0H361lE405gw01WR94v374288BJZClnt";


//    七位编码：NWqw3Zm
//    md5key :

    /**
     * 订单备案
     */
    public static String URL_ORDER_DECLARA = "http://180.166.132.109:8091/cepoi/order/orderlist.htm";

    /**
     *  商检企业备案
     */
    public static String URL_COMPANY_FILING = "http://180.166.132.110:8087/ciqInterface/company/companylist.htm";

    /**
     *  商检商品备案
     */
    public static String URL_CARGO_FILING = "http://180.166.132.110:8087/ciqInterface/cargo/cargolist.htm";



    public static String signMd5(String s, String privateKey){
        String valueStr = s+privateKey;
        String md5 = Md5Encoder.generateCode(valueStr).toUpperCase();
        System.out.println(md5);
        return md5;
    }


    public static JSONObject sendMsg(ACBTMsg ACBTMsg){
//        //验证
        List<String> validate = ValidatorUtil.validate(ACBTMsg);
        if(!validate.isEmpty()){
            throw new BusinessException(validate.toString());
        }

        String url = ACBTMsg.getReqUrl();
        if(ACBTMsg instanceof ACBTGetMsg){




        }else if(ACBTMsg instanceof ACBTPostMsg){
            //sign
            ACBTMsg.setCommitTime(DateUtil.dateToString(new Date(),"yyyyMMddHHmmss"));
            String s = ACBTMsg.toString();
            String sign = signMd5(s, CBTUtil.CBT_PRIVATE_KEY);

            Map<String,String> params = new HashMap<>();
            try {
                String data = URLEncoder.encode(s,"utf-8");
                params.put("EData",data );
                System.out.println(data);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
            params.put("SignMsg",sign);
            //发送post请求
            try {
                HttpResponse httpResponse = HttpUtil.doPost("", url, params);
                JSONObject json = HttpUtil.getJson(httpResponse);
                return json;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
             }
        }
        return null;
    }








    public static void main(String[] args){
        //订单申报test
        OrderDeclaraMsg orderDeclaraMsg = new OrderDeclaraMsg();
        orderDeclaraMsg.setAssBillNo("setAssBillNo");
        orderDeclaraMsg.setSenderName("setSenderName");
        orderDeclaraMsg.setOrderCommitTime("");

        String s = orderDeclaraMsg.toString();
        System.out.println(s);
        String s1 = signMd5(s, "91221");
        sendMsg(orderDeclaraMsg);
 //        JSONObject jsonObject = msgToJson(orderDeclaraMsg);
//        System.out.println(jsonObject);

//        JSONObject jsonObject = sendMsg(orderDeclaraMsg);
        //拿到json对象 保存订单申报记录


        //备案test




    }




}
